<?php

namespace app\admin\controller\training\config;

use app\BaseController;
use app\model\training\TrainingLecturerModel;
use app\model\training\TrainingTextbookModel;
use app\model\training\TrainingTextbookTypeModel;
use app\model\training\TrainingTypeModel;
use think\App;
use think\facade\Queue;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 讲师列表
     * @return bool|string
     */
    public function lecturerList(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require',
            ]);
            $TrainingLecturerModel = new TrainingLecturerModel();
            $query = $TrainingLecturerModel->where('unit_id','=',$this->userData['unit_id']);
            $data = $query->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 讲师详情
     * @return bool|string
     */
    public function lecturerInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $TrainingLecturerModel = new TrainingLecturerModel();
            $info = $TrainingLecturerModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 添加讲师
     * @return bool|string
     */
    public function lecturerAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'name|讲师名称' => 'require',
                'sex|性别' => 'require',
                'age|年龄' => 'require',
                'photo|照片' => 'require',
                'introduce|简介' => 'require',
                'label|标签' => 'require',
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['time'] = time();
            $TrainingLecturerModel = new TrainingLecturerModel();
            $TrainingLecturerModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


    /**
     * 编辑讲师
     * @return bool|string
     */
    public function lecturerUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $TrainingLecturerModel = new TrainingLecturerModel();
            $info = $TrainingLecturerModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除讲师
     * @return bool|string
     */
    public function lecturerDel(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $TrainingLecturerModel = new TrainingLecturerModel();
            $TrainingLecturerModel->where('id','in',$param['id'])->delete();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 教材类型
     * @return bool|string
     */
    public function textbookType(): bool|string
    {
        try {
            $TrainingTextbookTypeModel = new TrainingTextbookTypeModel();
            $data = $TrainingTextbookTypeModel
                ->where('unit_id','=',$this->userData['unit_id'])
                ->withCount(['TrainingTextbook'])
                ->order('sort','asc')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 添加教材类型
     * @return bool|string
     */
    public function textbookTypeAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'name|分类名称' => 'require',
                'sort|分类排序' => 'require'
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['time'] = time();
            $TrainingTextbookTypeModel = new TrainingTextbookTypeModel();
            $TrainingTextbookTypeModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 编辑教材类型
     * @return bool|string
     */
    public function textbookTypeUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
                'name|分类名称' => 'require',
                'sort|分类排序' => 'require'
            ]);
            $TrainingTextbookTypeModel = new TrainingTextbookTypeModel();
            $info = $TrainingTextbookTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除教材类型
     * @return bool|string
     */
    public function textbookTypeDel(): bool|string
    {
        $TrainingTextbookTypeModel = new TrainingTextbookTypeModel();
        $TrainingTextbookTypeModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $info = $TrainingTextbookTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->delete();
            $TrainingTextbookModel = new TrainingTextbookModel();
            $TrainingTextbookModel->where('type_id','=',$param['id'])->delete();
            $TrainingTextbookTypeModel->commit();
            return success();
        } catch (\Exception $e) {
            $TrainingTextbookTypeModel->rollback();
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 添加教材
     * @return bool|string
     */
    public function textbookAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'type_id|教材类型' => 'require',
                'file|PPT文件' => 'require',
                'cover|封面图' => 'require',
                'introduce|教材简介' => 'require',
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $TrainingTextbookModel = new TrainingTextbookModel();
            $id = $TrainingTextbookModel->insertGetId($param);
            Queue::push(\app\job\CronJob::class, [
                'method' => 'textbookConvertToPDF',
                'data' => [
                    'id' => $id,
                    'path' => $param['file']
                ],
            ], 'CronJob');
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 教材列表
     * @return bool|string
     */
    public function textbookList(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require'
            ]);
            $where = [];
            if (!empty($param['type_id'])){
                $where[] = ['type_id','=',$param['type_id']];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $TrainingTextbookModel = new TrainingTextbookModel();
            $query = $TrainingTextbookModel->where($where);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 教材详情
     * @return bool|string
     */
    public function textbookInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $TrainingTextbookModel = new TrainingTextbookModel();
            $info = $TrainingTextbookModel->with(['textbookType'])->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除教材
     * @return bool|string
     */
    public function textbookDel(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $TrainingTextbookModel = new TrainingTextbookModel();
            $TrainingTextbookModel->where('id','in',$param['id'])->delete();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 编辑教材
     * @return bool|string
     */
    public function textbookUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $TrainingTextbookModel = new TrainingTextbookModel();
            $info = $TrainingTextbookModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            Queue::push(\app\job\CronJob::class, [
                'method' => 'textbookConvertToPDF',
                'data' => [
                    'id' => $param['id'],
                    'path' => $param['file']
                ],
            ], 'CronJob');
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 修改教材启用状态
     * @return bool|string
     */
    public function textbookStatus(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
                'status|启用状态' => 'require|in:0,1'
            ]);
            $TrainingTextbookModel = new TrainingTextbookModel();
            $TrainingTextbookModel->where('id','in',$param['id'])->update(['status'=>$param['status']]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 讲座类型列表
     * @return bool|string
     */
    public function typeList(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require'
            ]);
            $TrainingTypeModel = new TrainingTypeModel();
            $query = $TrainingTypeModel->where('unit_id','=',$this->userData['unit_id']);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 添加讲座类型
     * @return bool|string
     */
    public function typeAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'name|类型名称' => 'require',
                'explain|类型说明' => 'require',
                'status|启用状态' => 'require|in:0,1'
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $TrainingTypeModel = new TrainingTypeModel();
            $is = $TrainingTypeModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['name','=',$param['name']],
            ])->value('id');
            if ($is) throw new \Exception('类型名称不能重复！');
            $TrainingTypeModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型详情
     * @return bool|string
     */
    public function typeInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $TrainingTypeModel = new TrainingTypeModel();
            $info = $TrainingTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 编辑类型
     * @return bool|string
     */
    public function typeUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $TrainingTypeModel = new TrainingTypeModel();
            $info = $TrainingTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $isArr = $TrainingTypeModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['name','=',$param['name']],
            ])->column('id');
            $isArr = array_diff($isArr,[$param['id']]);
            if (!empty($isArr)) throw new \Exception('类型名称不能重复！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除类型
     * @return bool|string
     */
    public function typeDel(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $TrainingTypeModel = new TrainingTypeModel();
            $TrainingTypeModel->where('id','in',$param['id'])->delete();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 修改类型启用状态
     * @return bool|string
     */
    public function typeStatus(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
                'status|启用状态' => 'require|in:0,1'
            ]);
            $TrainingTypeModel = new TrainingTypeModel();
            $TrainingTypeModel->where('id','in',$param['id'])->update(['status'=>$param['status']]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }
}